//
//  XHSearchBar.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/27.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "XHSearchBar.h"

@implementation XHSearchBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.placeholder = @"搜索";
        self.keyboardType = UIKeyboardTypeDefault;
        self.backgroundImage = [UIImage imageWithColor:UIColorHex(#F7F7F7)];
        //设置顶部搜索栏的背景色
        [self setBackgroundColor:[UIColor whiteColor]];
        [self xh_setCancelButtonTitleColor:kTitleColor];
        //设置顶部搜索栏输入框的样式
        UITextField *searchField = [self valueForKey:@"_searchField"];
        searchField.backgroundColor = UIColorHex(#F7F7F7);
        searchField.tintColor = kTitleColor;
        self.layer.cornerRadius = frame.size.height*.5;
        self.clipsToBounds = YES;
        [self setImage:IMAGE_NAMED(@"chatting_magnifying_glass") forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    }
    return self;
}

//下面两个方法是UISearchBar分类代码
- (void)xh_setTextColor:(UIColor *)textColor {
    if (@available(iOS 9.0, *)) {
        [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].textColor = textColor;
    }else {
        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:textColor];
    }
}

- (void)xh_setCancelButtonTitle:(NSString *)title {
    if (@available(iOS 9.0, *)) {
        [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:title];
    }else {
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:title];
    }
}
- (void)xh_setCancelButtonTitleColor:(UIColor*)titleColor{
    if (@available(iOS 9.0, *)) {
        [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:titleColor];
    }else {
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:titleColor];
    }
}
@end
